import { UICallbacks } from "db://oops-framework/core/gui/layer/Defines";
import { oops } from "db://oops-framework/core/Oops";
import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { UIID } from "../common/config/GameUIConfig";
import { Node } from "cc";
import { MainViewComp } from "./view/MainViewComp";
import { GameEvent } from "../../type/GameEvent";
import { AccountModelComp } from "./model/AccountModelComp";
import { smc } from "../common/ecs/SingletonModuleComp";
import { InitAccountComp } from "./bll/InitAccount";
import { Item } from "../item/Item";
/**
 * 账号模块
 * 1、连接游戏服务器
 * 2、登录玩家帐号，获取玩家信息
 * 3、断线重连接
 */
@ecs.register('Account')
export class Account extends ecs.Entity {
    //数据层
    AccountModel!: AccountModelComp;
    protected init() {
        this.addComponents<ecs.Comp>(
            AccountModelComp,
        );
    }


    destroy(): void {

        super.destroy();
    }
    login() {

        this.initAccount();
    }
    /** 初始化帐号信息 */
    initAccount() {
        if (smc.account.has(InitAccountComp)) {
            smc.account.remove(InitAccountComp)
        }
        smc.account.add(InitAccountComp);
    }
    /** 加载主界面 */
    async loadMain() {
        var callbacks: UICallbacks = {
            onAdded: (node: Node, params: any) => {
                this.add(node.getComponent(MainViewComp) as ecs.Comp);
            }
        };
        oops.gui.open(UIID.Main, null, callbacks);
    }
    /** 加载武将面 */
    async loadRolePage(callback: Function) {
        // var callbacks: UICallbacks = {
        //     onAdded: (node: Node, params: any) => {
        //         callback();
        //         this.add(node.getComponent(RolePageViewComp) as ecs.Comp);
        //     }
        // };
        // oops.gui.open(UIID.RolePage, null, callbacks);
    }
    /**
   * 更新背包道具数据
   * @param id 道具id
   * @param num 数量
   * @returns 
   */
    addItem(id: string, num: number) {
        //TODO: 添加道具
    }
    /** 检查上阵红点 */
    checkRpShangzhen() {
        //TODO: 检查上阵
    }
    /** 通过服务端数据初始化道具 */
    initItemByServer(data: any) {
        for (const id in data) {
            const num = data[id];
            if (num) {
                var item: Item | undefined = this.AccountModel.bag.get(id);
                if (item) {
                    item.ItemModel.num = num;
                    this.setSpecialItem(id, item.ItemModel.num);
                }
                else {
                    item = ecs.getEntity<Item>(Item);
                    item.ItemModel.id = id;
                    item.ItemModel.num = num;
                    this.AccountModel.bag.set(id, item);
                    this.setSpecialItem(id, item.ItemModel.num);
                }
            }
        }
    }

    setSpecialItem(id: string, num: number) {
        // 根据id判断物品类型

    }


    checkEquipUpgrade() {
        this.AccountModel.roles.forEach(role => {
            role.RoleModelEquip.equips.forEach(equip => {
                equip.EquipUpgradeCheck.check(equip);
            });
        });
    }
}